/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ZZ.java
 *
 * Created on Oct 26, 2010, 8:40:26 PM
 */

package Java;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author shyan
 */
public class ZZ extends javax.swing.JFrame {

    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String userID = "HR";
    private String userPW = "hr";
    private Connection con;
    private Statement stmt;
    private ResultSet rset;
    String MENU="";

    /** Creates new form ZZ */
    public ZZ() {
        super("ORDER CONFIRMATION");
        initComponents();
        setLocationRelativeTo(null);
        connectDatabase();
    }

    ZZ(String menu,String table)
    {
        super("ORDER CONFIRMATION");
        initComponents();
        setLocationRelativeTo(null);
        //connectDatabase();
        tableField.setText(table);
        menuField.setText(menu);
        MENU=menu;
        Link();
    }


    //***************************** DATEBASE CONNECTION PART ******************************
    private void connectDatabase()
    {
        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            con=DriverManager.getConnection(url, userID, userPW);
        }

        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(EMPLOYEE_PROFILE.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void Link()
    {
        connectDatabase();
        String SQL="SELECT * FROM ORDER_LIST WHERE MENU_CODE ='"+MENU+"'ORDER BY FOOD_CODE ASC";
        System.out.println(SQL);
        try
        {
            stmt=con.createStatement();
            rset=stmt.executeQuery(SQL);

            while(rset.next())
            {
               PC.setValueAt(rset.getString("FOOD_CODE")    ,rset.getRow()-1, 0);
               PC.setValueAt(rset.getString("FOOD_NAME")    ,rset.getRow()-1, 1);
               PC.setValueAt(rset.getString("FOOD_QUANTITY"),rset.getRow()-1, 2);
            }
            con.close();

        }
        catch(Exception ex)
        {
            System.out.print("failed");
            ex.printStackTrace();
        }
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        PC = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        tableLable = new javax.swing.JLabel();
        tableField = new javax.swing.JLabel();
        menuField = new javax.swing.JLabel();
        menuLable = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        wallpaper = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(600, 500));
        setResizable(false);

        jPanel1.setMinimumSize(new java.awt.Dimension(600, 500));
        jPanel1.setPreferredSize(new java.awt.Dimension(600, 500));
        jPanel1.setLayout(null);

        PC.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Food code", "Food name", "Unit ordered"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        PC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                PCMousePressed(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PCMouseClicked(evt);
            }
        });
        PC.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                PCMouseDragged(evt);
            }
        });
        jScrollPane1.setViewportView(PC);
        PC.getColumnModel().getColumn(0).setMinWidth(100);
        PC.getColumnModel().getColumn(0).setPreferredWidth(100);
        PC.getColumnModel().getColumn(0).setMaxWidth(100);
        PC.getColumnModel().getColumn(2).setMinWidth(100);
        PC.getColumnModel().getColumn(2).setPreferredWidth(100);
        PC.getColumnModel().getColumn(2).setMaxWidth(100);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(40, 160, 520, 240);
        jPanel1.add(jSeparator1);
        jSeparator1.setBounds(0, 66, 600, 10);

        jLabel1.setFont(new java.awt.Font("DejaVu Serif Condensed", 1, 24));
        jLabel1.setText("Order Confirmation");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(40, 20, 270, 30);

        tableLable.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        tableLable.setText("Table number :");
        jPanel1.add(tableLable);
        tableLable.setBounds(40, 80, 110, 30);

        tableField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        jPanel1.add(tableField);
        tableField.setBounds(160, 80, 80, 30);

        menuField.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        jPanel1.add(menuField);
        menuField.setBounds(160, 120, 80, 20);

        menuLable.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14));
        menuLable.setText("Menu code :");
        jPanel1.add(menuLable);
        menuLable.setBounds(40, 120, 100, 20);

        okButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton);
        okButton.setBounds(270, 440, 90, 27);

        cancelButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 13)); // NOI18N
        cancelButton.setText("CANCEL");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelButton);
        cancelButton.setBounds(370, 440, 90, 27);

        backButton.setFont(new java.awt.Font("DejaVu Serif Condensed", 0, 14)); // NOI18N
        backButton.setText("BACK");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        jPanel1.add(backButton);
        backButton.setBounds(470, 440, 90, 27);
        jPanel1.add(jSeparator2);
        jSeparator2.setBounds(0, 420, 600, 10);

        wallpaper.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/abstract2.jpg"))); // NOI18N
        jPanel1.add(wallpaper);
        wallpaper.setBounds(0, 0, 600, 500);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        
       String code="";
       String unit="";
       String query="";
       try
       {
           connectDatabase();
          // stmt = con.createStatement();;

           for( int j=0; j<50; j++)
           {
              if(PC.getValueAt(j, 0)!=null)
              {
                  unit=PC.getValueAt(j, 2).toString().trim();
                  code=PC.getValueAt(j, 0).toString().trim();
                  query="UPDATE ORDER_LIST SET FOOD_QUANTITY='"+unit+"'WHERE FOOD_CODE='"+code+"' AND MENU_CODE='"+MENU+"'";
                  System.out.println(query);
                  stmt=con.createStatement();
                  stmt.execute(query);
              }
           }
           JOptionPane.showMessageDialog(null,"Food order is dealed !");
           this.setVisible(false);
           new FIRST_SCREEN().setVisible(true);
       
       }
       catch (Exception ex)
       {
            System.out.println("Failed");
            ex.printStackTrace();
       }
             
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed

        String detect=PC.getValueAt(0, 0).toString();
        if(detect==null)
        {
            JOptionPane.showMessageDialog(null, "Nothing to be deleted !");
        }
        else
        {

            String message = "Are you sure want to delete the order ?";
            String title1 = "Confirmation?";
            int reply = JOptionPane.showConfirmDialog(null, message, title1, JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION)
            {
              String Query="DELETE FROM ORDER_LIST WHERE MENU_CODE="+MENU;
              try
              {
                  connectDatabase();
                  stmt=con.createStatement();
                  stmt.execute(Query);
                  con.close();
                  JOptionPane.showMessageDialog(null, "Menu order "+MENU+" has been deleted !");
                  this.setVisible(false);
                  new MENU_LOGIN().setVisible(true);
              }
              catch(Exception ex)
              {
                  ex.printStackTrace();
              }

            }
            else
            {
                System.out.println("=.= wee wang wang");
            }
        }
        
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void PCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PCMouseClicked
 
    }//GEN-LAST:event_PCMouseClicked

    private void PCMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PCMouseDragged
        
        int row = PC.getSelectedRow();
        String menuCode=menuField.getText().trim();
        String detect = (String) PC.getValueAt(row, 0);

        if(detect==null)
        {
            System.out.println("=.= sigh");
        }
        else
        {
            String message = "Delete the order "+detect;
            String title1 = "Confirmation?";
            int reply = JOptionPane.showConfirmDialog(null, message, title1, JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION)
            {
                 String query="DELETE FROM ORDER_LIST WHERE MENU_CODE='"+menuCode+"'AND FOOD_CODE='"+detect+"'";
                 System.out.println(query);
                 try
                 {
                    connectDatabase();
                    stmt=con.createStatement();
                    stmt.execute(query);
                    con.close();
                 }
                 catch(Exception ex)
                 {
                    ex.printStackTrace();
                 }
                 resetTable();
                 Link();
            }
            else
            {
                System.out.println("lalala ==");
            }
        }
        
    }//GEN-LAST:event_PCMouseDragged

    private void PCMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PCMousePressed

    }//GEN-LAST:event_PCMousePressed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.setVisible(false);
        try {
            new MENU_SELECTION_MAIN().setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(ZZ.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_backButtonActionPerformed


    void resetTable()
   {
       for (int row = 0; row < 50; row++)
       {
           for (int col = 0; col < 3; col++)
           {
                PC.setValueAt(null,row,col);
           }
       }
    }



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ZZ().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable PC;
    private javax.swing.JButton backButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel menuField;
    private javax.swing.JLabel menuLable;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel tableField;
    private javax.swing.JLabel tableLable;
    private javax.swing.JLabel wallpaper;
    // End of variables declaration//GEN-END:variables

}
